(function(window) {
	var userName, devSN, shopName;
	var findoption = {
		shopName: "",
		msgBody: ""
	}
	function openViewWithUrl(url, id) {
		commonTool.openWindow(url, id, {});
	} 
	
	function messagePagePulldownRefresh(isPull){
		if(isPull === undefined) {
			isPull = true;
		}
		var starTime = '';
		var endTime = new Date().getTime();
		starTime = endTime - 7 * 24 * 60 * 60 * 1000;
		getOasisAlarmList(isPull,userName, starTime, endTime, 10, 1,findoption, function() {
			vm.oasisAlarmInfo = {};
			if(isPull !== false){
				$('#message-scroll').pullRefresh().endPulldownToRefresh();
			}
		});
	}
	function getOasisAlarmList(isPull,userName, starTime, endTime, pageSize,currentPage,findoption, callback){
		v3AceOasisMsg.getOasisAlarmList(userName, starTime, endTime, pageSize, currentPage, findoption,function(code, data) {
			if(code === 0){
				if(data){
					var countLength = data.sendmsgList.length;
					vm.alarmTotalCount = data.totalCount === 0 ? countLength : data.totalCount;
					vm.oasisAlarmInfo = data.sendmsgList;
					if(vm.oasisAlarmInfo.length == vm.alarmTotalCount) {
						vm.isActive1 = true;
						vm.showMoreTip1 = "没有更多数据了";
					} else {
						vm.isActive1 = false;
						vm.showMoreTip1 = "加载更多";
					};
				}
			}else {
				if(isPull !== false) {
					$('#message-scroll').pullRefresh().endPulldownToRefresh();
				}
			}
		});
	}
	function loadMoreMsgList(currentPage){
		vm.currentPage = currentPage +1;
		var starTime = '';
		var endTime = new Date().getTime();
		starTime = endTime - 7 * 24 * 60 * 60 * 1000;
		v3AceOasisMsg.getOasisAlarmList(userName, starTime, endTime, 10, vm.currentPage, findoption, function(code, data) {
			if(code === 0){
				var countLength = data.sendmsgList.length;
					vm.alarmTotalCount = data.totalCount === 0 ? countLength : data.totalCount;
				if(data.sendmsgList){
					data.sendmsgList.forEach(function(item) {
						vm.oasisAlarmInfo.push(item);
					});
					if(vm.oasisAlarmInfo.length == vm.alarmTotalCount) {
						vm.isActive1 = true;
						vm.showMoreTip1 = "没有更多数据了";
					} else {
						vm.isActive1 = false;
						vm.showMoreTip1 = "加载更多";
					};
				}
			}
		});
	}
	
	function showMsgInfo(msgInfo){
		commonTool.openWindow("../../dist/new/message/message-info.html", "../../dist/new/message/message-info.html", {
			msgInfo: msgInfo
		});
	}

	function systemPagePulldownRefresh(isPull){
		if(isPull === undefined) {
			isPull = true;
		}
		getSystemMsgList(isPull,userName,2,1,10,function(){
			vm.systemMsgInfo = {};
			if(isPull !== false){
				$('#system-scroll').pullRefresh().endPulldownToRefresh();
			}
		});
	}
	
	function getSystemMsgList(isPull,userName,notify_location,start,size,callback){
		v3AceOasisMsg.getOasisSystemMsg(userName, notify_location,start,size, function(code, data) {
			if(code === 0){
				if(data){
					var countLength = data.rowCount;
				    vm.systemTotalCount = data.rowCount === 0 ? countLength : data.rowCount;
				    if(data.data){
				    	vm.systemMsgInfo = data.data;
						if(vm.systemMsgInfo.length == vm.systemTotalCount){
							vm.isActive2 = true;
							vm.showMoreTip2 = "没有更多数据了";
						} else {
							vm.isActive2 = false;
							vm.showMoreTip2 = "加载更多";
						};
				    }else {
						vm.isActive2 = false;
						vm.showMoreTip2 = "没有更多数据了";
					};
				}
			}else {
				if(isPull !== false) {
					$('#message-scroll').pullRefresh().endPulldownToRefresh();
				}
			}
		});
	}
	
	function loadMoreSysmsgList(Sysstart){
		vm.Sysstart = Sysstart + 10;
		v3AceOasisMsg.getOasisSystemMsg(userName, 2, vm.Sysstart, 10, function(code, data) {
			if(code === 0){
				var countLength = data.rowCount;
				vm.systemTotalCount = data.rowCount === 0 ? countLength : data.rowCount;
				if(data.data){
					data.data.forEach(function(item) {
						vm.systemMsgInfo.push(item);
					});
					if(vm.systemMsgInfo.length == vm.systemTotalCount){
						vm.isActive2 = true;
						vm.showMoreTip2 = "没有更多数据了";
					} else {
						vm.isActive2 = false;
						vm.showMoreTip2 = "加载更多";
					};
				}
			}
		});
	}
	
	function showSystemMsgInfo(systemMsgInfo){
		commonTool.openWindow("../../dist/new/message/systemMsg-info.html", "../../dist/new/message/systemMsg-info.html", {
			systemMsgInfo: systemMsgInfo
		});
	}
	
	function getUserId(){
		v3AceOasisMsg.getUserId(userName,function(code, data){
			if(code === 0){
				if(data){
					vm.userId = data.id;
					operationPagePulldownRefresh(false);
				}
			}
		});
	}
	
	function operationPagePulldownRefresh(isPull){
		if(isPull === undefined) {
			isPull = true;
		}
		var userId = vm.userId;
		var starTime = '';
		var endTime_get = new Date().getTime();
		var endTime = $.util.formatDate(endTime_get);
		var starTime_get = endTime_get - 7 * 24 * 60 * 60 * 1000;
		starTime = $.util.formatDate(starTime_get);
		getOperationLogList(userName,userId,starTime, endTime, 1, 10, function(){
			vm.OperationLogInfo = {};
			if(isPull !== false){
				$('#operation-scroll').pullRefresh().endPulldownToRefresh();
			}
		});
	}
	
	function getOperationLogList(userName,userId,starTime, endTime, start, size, callback){
		v3AceOasisMsg.getOperationLog(userName,userId,starTime,endTime,start,size, function(code, data) {
			if(code === 0){
				if(data){
					var countLength = data.total;
					vm.operationTotalCount = data.total === 0 ? countLength : data.total;
					vm.OperationLogInfo = data.rows;
					if(vm.OperationLogInfo.length == vm.operationTotalCount) {
						vm.isActive3 = true;
						vm.showMoreTip3 = "没有更多数据了";
					} else {
						vm.isActive3 = false;
						vm.showMoreTip3 = "加载更多";
					};
				}
			}
		});
	}
	
	function loadMoreOperList(Operstart){
		vm.Operstart = Operstart + 1;
		var userId = vm.userId;
		var starTime = '';
		var endTime_get = new Date().getTime();
		var endTime = $.util.formatDate(endTime_get);
		var starTime_get = endTime_get - 7 * 24 * 60 * 60 * 1000;
		starTime = $.util.formatDate(starTime_get);
		v3AceOasisMsg.getOperationLog(userName,userId,starTime,endTime,vm.Operstart,10, function(code, data) {
			if(code === 0){
				if(data){
					var countLength = data.total;
					vm.operationTotalCount = data.total === 0 ? countLength : data.total;
					if(data.rows){
						data.rows.forEach(function(item) {
							vm.OperationLogInfo.push(item);
						});
						if(vm.OperationLogInfo.length == vm.operationTotalCount) {
							vm.isActive3 = true;
							vm.showMoreTip3 = "没有更多数据了";
						} else {
							vm.isActive3 = false;
							vm.showMoreTip3 = "加载更多";
						};
					}
				}
			}
		});
	}
	
	function showOperationInfo(operationInfo){
		commonTool.openWindow("../../dist/new/message/operation-info.html", "../../dist/new/message/operation-info.html", {
			operationInfo: operationInfo
		});
	}
	
	window.addEventListener('selectMsgToIndex',function(event){
		
	});

	function mounted() {
		mui.init({
			swipeBack: true
		});
		mui.plusReady(function() {
			userName = dataManager.getUserName();
			vm.subPageHeight = screen.height - (44 + 38 + 20) + "px";
			vm.$nextTick(function() {
				$('.mui-scroll-wrapper').scroll({
					indicators: true, //是否显示滚动条
					bounce: false,
					deceleration: 0.0005
				});
				$("#message-scroll").pullToRefresh({
					down: {
						callback: messagePagePulldownRefresh
					}
				});
				$("#system-scroll").pullToRefresh({
					down: {
						callback: systemPagePulldownRefresh
					}
				});
				$("#operation-scroll").pullToRefresh({
					down: {
						callback: operationPagePulldownRefresh
					}
				});
			});
			getUserId();
			messagePagePulldownRefresh(false);
			systemPagePulldownRefresh(false);
			
		});
	}

	window.message = {
		mounted: mounted,
		data: {
			subPageHeight: '0px',
			currentPage: 1,
			Sysstart: 1,
			Operstart: 1,
			userId : 0,
			alarmTotalCount: 0,
			systemTotalCount: 0,
			operationTotalCount: 0,
			showMoreTip1: "",
			showMoreTip2: "",
			showMoreTip3: "",
			isActive1: false,
			isActive2: false,
			isActive3: false,
			oasisAlarmInfo: [],
			systemMsgInfo: [],
			OperationLogInfo: [],
		},
		methods: {
			openViewWithUrl: openViewWithUrl,
			loadMoreMsgList: loadMoreMsgList,
			loadMoreSysmsgList: loadMoreSysmsgList,
			loadMoreOperList: loadMoreOperList,
			showMsgInfo: showMsgInfo,
			showSystemMsgInfo: showSystemMsgInfo,
			showOperationInfo: showOperationInfo,
			selectMsgWithCondition: function(){
				commonTool.openWindow('../../dist/new/message/searchmsg.html','searchmsg.html')
			}
		}
	};
})(window);